//
//  ChatUpMessageModel.swift
//  Chatup
//
//  Created by op on 2023/10/17.
//

import Foundation
import KakaJSON
import WCDBSwift
// import Alamofire

public protocol DYColumnCodable: ColumnCodable, ConvertibleEnum {}

extension DYColumnCodable {
    static var columnType: ColumnType {
        return .integer64
    }
}
class MsgModel: ChatUpDBBaseModel, ChatUpDBStorageProtocol {
    enum RoleType: Int, DYColumnCodable {
        case user = 0
        case bot = 1

        init?(with value: FundamentalValue) {
            let data = value.int64Value
            self = RoleType(rawValue: Int(data)) ?? .user
        }

        func archivedValue() -> FundamentalValue {
            return FundamentalValue(rawValue)
        }
    }

    enum MessageType: Int, DYColumnCodable {
        case text = 0
        case image = 1
        case networkImage = 2
        case file = 3

        init?(with value: FundamentalValue) {
            let data = value.int64Value
            self = MessageType(rawValue: Int(data)) ?? .text
        }

        func archivedValue() -> FundamentalValue {
            return FundamentalValue(rawValue)
        }
    }

    var messageId: Int64? = nil
//    var questionId: Int64 = 0
    var type: RoleType = .user
    var messageType: MessageType = .text
    var message: String = ""
    var context: String?
    var roleId: Int = 0
    var createTime: Int64? // 创建时间
    var isLike: Bool = false
    var isUnlike: Bool = false
    convenience init(type: RoleType, messageType: MessageType, message: String, roleId: Int, isLike: Bool, isUnlike: Bool, context: String? = nil) {
        self.init()
//        self.questionId = questionId
        self.type = type
        self.message = message
        self.roleId = roleId
        self.createTime = kCurentTimeInt()
//        self.messageId = kCurentTimeInt()
        self.isLike = isLike
        self.isUnlike = isUnlike
        self.messageType = messageType
        self.context = context
    }

    /** < 获取当前系统时间戳 10位 秒 */
    func kCurentTimeInt() -> Int64 {
        return Int64(round(Date().timeIntervalSince1970 * 1000))
    }

    // MARK: 以类名作为表名 需要修改重写该方法

    override func tableName() -> String? {
        return "msg_db"
    }

    enum CodingKeys: String, CodingTableKey {
        typealias Root = MsgModel
        static let objectRelationalMapping = TableBinding(CodingKeys.self)

        case messageId
        case message
        case type
        case roleId
        case createTime
//        case questionId
        case isLike
        case isUnlike
        case messageType
        case context

        static var columnConstraintBindings: [CodingKeys: ColumnConstraintBinding]? {
            return [messageId: ColumnConstraintBinding(isPrimary: true, isAutoIncrement: true)]
        }
    }
}

extension MsgModel {
    func isBot() -> Bool {
        return type == .bot
    }

    func isUser() -> Bool {
        return type == .user
    }

    func isText() -> Bool {
        return messageType == .text
    }

    func isImage() -> Bool {
        return messageType == .image || messageType == .networkImage
    }
    
    func isFile() -> Bool {
        return messageType == .file
    }
}
